package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.manager.feign.PrintWorkOrderFeignClient;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.PrintWorkOrderDTO;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Locale;

/**
 * @author guohui.zhang
 * @Title: 工单支持打印
 * @date 2020/6/2312:08
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/print")
public class PrintWorkOrderController {
    @Resource
    private PrintWorkOrderFeignClient printWorkOrderFeignClient;
    @Resource
    private ProBaseConfig proBaseConfig;

    /**
     * 工单支持打印
     * @param printWorkOrderDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addDefaultPrintTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addDefaultPrintTemplate(@RequestBody @Valid PrintWorkOrderDTO printWorkOrderDTO) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(printWorkOrderDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        Locale locale = LocaleContextHolder.getLocale();
        String lang = locale.toString();
        XbbResponse<JSONObject> install = printWorkOrderFeignClient.install(printWorkOrderDTO, lang);
        JSONObject result = install.getResult();
        return result.toJSONString();
    }
}
